import { IsInt, IsOptional, Min } from 'class-validator';
import { Transform, Type } from 'class-transformer';

export class PaginationBaseReqDto {
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Transform(({ value }) => (value !== undefined ? +value : 1))
  page: number;

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Transform(({ value }) => (value !== undefined ? +value : 20))
  pageSize: number;

  constructor() {
    this.page = this.page || 1;
    this.pageSize = this.pageSize || 20;
  }

  get skip(): number {
    return (this.page - 1) * this.pageSize;
  }

  get take(): number {
    return this.pageSize;
  }
}
